$(document).ready(function()
{ 
  //---------------------------------------
  // put all your jQuery goodness in here.
  

    //free text watermark
    $("#clientorder_Free_text_text").watermark("Bitte geben Sie hier Ihren kurzen Erklärungstext für den Autoren ein");
//    
//    {
//          html: 'Bitte geben Sie hier Ihren kurzen Erklärungstext für den Autoren ein'
//          css:{
//            color: 'gray'
//          }
//        });


    //Own Choice text
    var ownChoiceText = "hide";
    $("#clientorder_Edition_options_Own_choice_text").hide();
     
    if ($("#clientorder_Edition_options_Own_choice_text").val() != ""){
        ownChoiceText = "show";
        $("#clientorder_Edition_options_Own_choice_is_own_choice").prop("checked", true);
        $("#clientorder_Edition_options_Own_choice_text").show("slow");
    }
    
    $("#clientorder_Edition_options_Own_choice_is_own_choice[type=checkbox]").click(function ( event ) {

      if (ownChoiceText == "show"){
        $("#clientorder_Edition_options_Own_choice_text").hide("slow");
        $("#clientorder_Edition_options_Own_choice_text").val("");
        $("#clientorder_Edition_options_Own_choice_text").watermark({remove:true});
        ownChoiceText = "hide";
      }
      else{
        $("#clientorder_Edition_options_Own_choice_text").show("slow");
        ownChoiceText = "show";
        $("#clientorder_Edition_options_Own_choice_text").watermark('Eigene Wahl');
//          html: 'Eigene Wahl', 
//          css:{
//            color: 'gray'
//          }
//        });
      }
    });
    
    
    //Text Media
    var otherText = "hide";
    $("#clientorder_Other_text_media_name").hide();
     
    if ($("#clientorder_Other_text_media_name").val() != ""){
        otherText = "show";
        $("#clientorder_Other_text_media_otherText").prop("checked", true);
        $("#clientorder_Other_text_media_name").show("slow");
    }

    $("#clientorder_Other_text_media_otherText[type=checkbox]").click(function ( event ) {

      if (otherText == "show"){
        $("#clientorder_Other_text_media_name").watermark({remove:true});
        $("#clientorder_Other_text_media_name").val("");
        $("#clientorder_Other_text_media_name").hide("slow");
        otherText = "hide";
        
      }
      else{
        $("#clientorder_Other_text_media_name").show("slow");
        otherText = "show";
        
        $("#clientorder_Other_text_media_name").watermark({
          html: 'Kurzbeschreibung', 
          css:{
            color: 'gray'
          }
        });
      }
    });
    
    
    //Standard order
    var standardOrder = "hide";
    $("#edOptions").hide();
    
    if ($("#clientorder_is_standard").prop("checked") == false){
        standardOrder = "show";
        $("#edOptions").show("slow");
        
        //when is loading for the first time, the options come from the DDBB
        
    }

    $("#clientorder_is_standard[type=checkbox]").click(function ( event ) {

      if (standardOrder == "show"){
        $("#edOptions").hide("slow");
        standardOrder = "hide";
      }
      else{
        $("#edOptions").show("slow");
        standardOrder = "show";
        
        //all option default to yes:
        $("#clientorder_Edition_options_is_technical_terms_0").attr('checked', false);
        $("#clientorder_Edition_options_is_technical_terms_1").attr('checked', true);

        $("#clientorder_Edition_options_is_foraign_word_0").attr('checked', false);
        $("#clientorder_Edition_options_is_foraign_word_1").attr('checked', true);

        $("#clientorder_Edition_options_is_superlative_0").attr('checked', false);
        $("#clientorder_Edition_options_is_superlative_1").attr('checked', true);

        $("#clientorder_Edition_options_is_old_language_0").attr('checked', false);
        $("#clientorder_Edition_options_is_old_language_1").attr('checked', true);

        $("#clientorder_Edition_options_is_start_of_sentence_0").attr('checked', false);
        $("#clientorder_Edition_options_is_start_of_sentence_1").attr('checked', true);
        
//        $("#clientorder_Edition_options_moreText").watermark({
//          html: 'Nennen Sie Wörter, die Sie ausschließen möchten. Trennen Sie mit Komma.', 
//          css:{
//            color: 'gray'
//          }
//        });
      }
    });
    
    
    
    //slider-------------------------------------

    


     //Formulate text
     $("#clientorder_formulateId").selectToUISlider({
////      slide: function( event, ui ) {
////				$( "#amount" ).val( "XX" );
////                alert($( "#amount" ));
////			}
     }
      );
        
//
//     
     $("#clientorder_formulateId").hide();//hide the select itself 
     
     //emotion style
     $("#clientorder_emotionStyleId").selectToUISlider({ 
     
//     change: function(event, ui) { 
//        $("#clientorder_emotionStyleId")
//     }
   
     });
     $("#clientorder_emotionStyleId").hide();//hide the select itself

     //Person fomr
     
     $("#clientorder_personStyleId").selectToUISlider(
          sliderOpts = {
          stop: function(e,ui) { 
//              var v = $("#clientorder_personStyleId").attr('aria-valuenow');
//              alert(v);
              alert("j");
          }}
      );
     $("#clientorder_personStyleId").hide();//hide the select itself

     //Style
     $("#clientorder_formalStyleId").selectToUISlider({});
     $("#clientorder_formalStyleId").hide();//hide the select itself
     
    
    //slider-------------------------------------

    //alert when not save and leaving
     $("#back2List").click(function ( event ) {
//       alert("back2List");
       if (! confirm('Are you sure to leave')) 
       {
         return false;
       }
       
    });
    
    
    
    //alert when trying to save and there is nothing written in the checked checkboxes
    $("#saveButton").click(function ( event ) {
//       alert("saveButton -  clicked ="+ clicked);

       //check the other text media option
       if ($("#clientorder_Other_text_media_otherText").prop("checked") == true){
         if($("#clientorder_Other_text_media_name").val() == ""){
           alert("Please check: Other text media is selected but nothing was written.");
           return false;
         }
       }
       

       //check the own choice
       if ($("#clientorder_is_standard").prop("checked") == false){//is it not an standard order?
         
         if ($("#clientorder_Edition_options_Own_choice_is_own_choice").prop("checked") == true){
           
           if($("#clientorder_Edition_options_Own_choice_text").val() == ""){
             alert("Please check: Own choice is selected but nothing was written.");
             return false;
           }
         }

       }
       
       //if it is an standard order, all edition options should be false/zero/empty
       if ($("#clientorder_is_standard").prop("checked") == true){//is it an standard order?
     
            //technical terms  
            $("#clientorder_Edition_options_is_technical_terms_0").attr('checked', true);
            $("#clientorder_Edition_options_is_technical_terms_1").attr('checked', false);
            
            $("#clientorder_Edition_options_is_foraign_word_0").attr('checked', true);
            $("#clientorder_Edition_options_is_foraign_word_1").attr('checked', false);
            
            $("#clientorder_Edition_options_is_superlative_0").attr('checked', true);
            $("#clientorder_Edition_options_is_superlative_1").attr('checked', false);
            
            $("#clientorder_Edition_options_is_old_language_0").attr('checked', true);
            $("#clientorder_Edition_options_is_old_language_1").attr('checked', false);
            
            $("#clientorder_Edition_options_is_start_of_sentence_0").attr('checked', true);
            $("#clientorder_Edition_options_is_start_of_sentence_1").attr('checked', false);
            
            //own choice
            $("#clientorder_Edition_options_Own_choice_is_own_choice").prop("checked", false);
            $("#clientorder_Edition_options_Own_choice_text").val("");
            
            //more text
            $("#clientorder_Edition_options_moreText").val("");
            
       }
       
      
       
    });//save



    //-------------autosave--------------------
    //autosave automatically every X seconds
    
//    var clicked = true;
//    $("#formB").submit(function(e,clic){
//      if (clic == false){
//        clicked = true;
//        $('#formB').submit();
//
//        return false;
//      }
//    });
//   
//      var time = 6000;//6 seconds
//      setInterval(function() {
//
//          clicked = false; 
//          $("#formB").submit(clicked);
//      }, time);
      
      
      
      
//    var clicked = true;
//    $("#formB").submit(function(e,clic){
//      //Prevent form submit: 
////      e.preventDefault()
//      if (clic == false){
//        clicked = true;
//        $('#formB').submit();
//
//        return false;
//      }
//  
//    });
//
//   
//      var time = 6000;//6 seconds
//      setInterval(function() {
//          
//        
//          // Do something every time seconds
//           //alert("bien!");
//           
//          clicked = false; 
////          $("#saveButton").click();
//          $("#formB").submit(clicked);
////          return false;
//      }, time);
      
        
      

    
     //setInterval(function, delay);

//     var timeout_text;
//     function timeout_init() {
//    timeout_text = setTimeout('timeout_trigger()', 3000);
//    document.getElementById('timeout_text').innerHTML = 'The timeout has been started';
//}
//    function timeout_trigger() {
//        document.getElementById('timeout_text').innerHTML = 'The timeout has been triggered';
//    }
      
      
//      setTimeout(function(){  
//                alert("??");  
//            }, time);  
//    jQuery.fn.idle = function(time){  
//        var i = $(this);  
//        i.queue(function(){  
//            setTimeout(function(){  
//                i.dequeue();  
//            }, time);  
//        }); 
//        alert("???");
//    };  
    
//    $('.myelement').fadeIn().idle(2000).fadeOut('slow');  

     
//    $("form#formB").autosave({
//      
//      
////      callbacks: {
////          trigger: ["change", function() {
////            var self = this;
////
////            $("[name=save]").click(function() {
////              self.save();
////            });
////          }]
////      }
//      //url: "www.google.com",
////      moduleName: "order",
//      method: "get",
//      data: "moduleName=order"
////      send: function() {
////        // Do stuff while we wait for the ajax response, defaults to doing nothing
////        alert("enviando");
////      }
//      
//      
//    });


//    function autosave() 
//    { 
//      alert("autosave");
//      
//      var t = setTimeout("autosave()", 20000); //every 20 seconds (20000 miliseconds)
//      var orderName = $("#clientorder_orderName").val(); 
//      if (orderName.length > 0)
//      {
////         $.ajax( 
////        { 
//          alert(orderName);
////            type: "POST", 
////            url: "autosave.php", 
////            data: "id=" + <?php echo $article_id ?> 
////+ "&title=" + title + "&text=" + text + "&author=" + author + "&issue=" + issue + "&category=" + category + "&date=" + date + "&imageURL=" + image, 
////            cache: false, 
////            success: function(message) 
////            { 
////                $("#timestamp").empty().append(message); 
////            } 
////        }); 
//      }
//    }
    
    //-------------autosave--------------------
    //test  
//    alert(" :) ");



  //---------------------------------------
});
